package com.example.carefree_server.controller.c_loginController;

import com.example.carefree_server.entity.Department;
import com.example.carefree_server.entity.Result;
import com.example.carefree_server.entity.Student;
import com.example.carefree_server.service.s_loginService.LoginService;
import com.example.carefree_server.utils.JwtUtil;
import com.example.carefree_server.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/logins")
public class LoginController {
    String ID = null;

    @Autowired
    private LoginService login;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/login")
    public Result login(String id, String password, String code) {
        ID = id;
        ValueOperations<String, String> Operations = stringRedisTemplate.opsForValue();
        code = code.toLowerCase();
        String rediscode = Operations.get(code);
        if (rediscode == null) {
            return Result.error("验证码错误！");
        }
//        根据用户名查询用户
        password = Md5Util.getMD5String(password);
        System.out.println(password);
        int loginuser = login.findstudent(id, password);
        if (loginuser == 0) {
            return Result.error("账号或密码错误");
        }
        //如果是学生登录成功
        if (loginuser == 1) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", id);
            String token = JwtUtil.genToken(claims);
            Operations.set(token, token, 100, TimeUnit.HOURS);//重载
            return Result.losuccess(1, token);
        } else if (loginuser == 2) {//如果是部门登录成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", id);
            String token = JwtUtil.genToken(claims);
            Operations.set(token, token, 100, TimeUnit.HOURS);//重载
            //创建返回data容器，用于返回
            return Result.losuccess(2, token);
        } else {//如果是管理员登录成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", id);
            String token = JwtUtil.genToken(claims);
            Operations.set(token, token, 100, TimeUnit.HOURS);//重载
            //创建返回data容器，用于返回
            return Result.losuccess(3, token);
        }
    }

    //忘记密码

    //获得账号
    @PostMapping("/GetId")
    public Result GetId(String id) {
        ID = id;
        System.out.println("GetId是："+id);
        int loginuser = login.GetId(id);

        if (loginuser == 0) {
            return Result.error("无此账号");
        }
        //如果是学生验证成功
        if (loginuser == 1) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", id);
            String token = JwtUtil.genToken(claims);
            ValueOperations<String, String> Operations = stringRedisTemplate.opsForValue();//创建redis容器
            Operations.set(token, token, 100, TimeUnit.HOURS);//重载
            return Result.checksuccess(1, token);
        } else if (loginuser == 2) {//如果是部门验证成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", id);
            String token = JwtUtil.genToken(claims);
            ValueOperations<String, String> Operations = stringRedisTemplate.opsForValue();//创建redis容器
            Operations.set(token, token, 100, TimeUnit.HOURS);//重载
            return Result.checksuccess(2, token);
        }
        return Result.error("无此id");
    }

    //获得问题
    @GetMapping("/StudentQuestion1")
    public List<Student> StudentQuestion1() {
        String sid=ID;
        System.out.println("学生的id是："+sid);
        return login.getsquestion1(sid);
    }
    @GetMapping("/DepartmentQuestion1")
    public List<Department> DepartmentQuestion1() {
        String did=ID;
        System.out.println("部门的id是："+did);
        return login.getdquestion1(did);
    }

    //    审核密保
    @PostMapping("/CheckPasswordprotection")
    public Result CheckPasswordprotection(String protection1) {
        String id = ID;
        int loginuser = login.checkprotection(id, protection1);
        if (loginuser == 0) {
            return Result.error("密保错误");
        }
        //如果是学生验证成功
        if (loginuser == 1) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", id);
            String token = JwtUtil.genToken(claims);
            ValueOperations<String, String> Operations = stringRedisTemplate.opsForValue();//创建redis容器
            Operations.set(token, token, 100, TimeUnit.HOURS);//重载
            return Result.checksuccess(1, token);
        } else if (loginuser == 2) {//如果是部门验证成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", id);
            String token = JwtUtil.genToken(claims);
            ValueOperations<String, String> Operations = stringRedisTemplate.opsForValue();//创建redis容器
            Operations.set(token, token, 100, TimeUnit.HOURS);//重载
            return Result.checksuccess(2, token);
        }
        return Result.error("无此id");
    }

    //    修改密码
    @PutMapping("/changepassword")
    public Result changepassword(String password) {
        String id = ID;
        password = Md5Util.getMD5String(password);
        System.out.println(password);
        int loginuser = login.changepassword(id, password);

        //如果是学生更改成功
        if (loginuser == 1) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", id);
            String token = JwtUtil.genToken(claims);
            ValueOperations<String, String> Operations = stringRedisTemplate.opsForValue();//创建redis容器
            Operations.set(token, token, 100, TimeUnit.HOURS);//重载
            return Result.changepasswordsuccess(1, token);
        } else if (loginuser == 2) {//如果是部门更改成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", id);
            String token = JwtUtil.genToken(claims);
            ValueOperations<String, String> Operations = stringRedisTemplate.opsForValue();//创建redis容器
            Operations.set(token, token, 100, TimeUnit.HOURS);//重载
            return Result.changepasswordsuccess(2, token);
        }

        return Result.error("无此id");
    }

    //    修改密保
    @PutMapping("/ChangePasswordProtection")
    public Result ChangePasswordProtection(String question1, String protection1) {
        String id = ID;
        System.out.println(id);
        int loginuser = login.changepasswordprotection(id, question1, protection1);
        if (loginuser == 0) {
            return Result.error("改问题或密保失败");
        }
        //如果是学生更改密保成功
        if (loginuser == 1) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", id);
            String token = JwtUtil.genToken(claims);
            ValueOperations<String, String> Operations = stringRedisTemplate.opsForValue();//创建redis容器
            Operations.set(token, token, 100, TimeUnit.HOURS);//重载
            return Result.changepasswordprotectionsuccess(1, token);
        } else if (loginuser == 2) {//如果是部门更改密保成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", id);
            String token = JwtUtil.genToken(claims);
            ValueOperations<String, String> Operations = stringRedisTemplate.opsForValue();//创建redis容器
            Operations.set(token, token, 100, TimeUnit.HOURS);//重载
            return Result.changepasswordprotectionsuccess(2, token);
        }

        return Result.error("无此id");
    }


}